<?php

function dislplayEventUserImage($sUser)
{
	$oRespX = new xajaxResponse();
	
	$aParams = array
	(
		'sUser' => $sUser,
		'iSize' => 75,
		'class' => 'pic1',
		'onmouseover' => "showInfo5(\'" . $sUser . "\', \'\', \'\');this.className=\'pic2\';",
		'onmouseout' => "this.className=\'pic1\';return nd();"
	);
	
	$oAcc =& App::getModuleComponent('Account', 'Avatar', $aParams);
	
	$sImage = $oAcc->process();
		
	$oRespX->addScript("sId('user_name').innerHTML='<a href=\"" . App::makeUrl('public.user', array('name' => $sUser)) . "\" target=\"_blank\">" . $sUser . "</a>';");
	$oRespX->addScript("sId('user_pic').innerHTML='<div style=\"padding:4px;\"><a href=\"" . App::makeUrl('public.user', array('name' => $sUser)) . "\" target=\"_blank\">" . $sImage . "</a></div>';");
	
	return $oRespX;
}


function newEventComment($aFormValue)
{
	isUser();

	$oRespX = new xajaxResponse();

	if ( empty($aFormValue['alert_id']) )
	{
		$oRespX->addScript("alert('". App::format('Mod_Comments.error.no_alert_id') ."')");
		$oRespX->addScript("sId('comment_submit_button').disabled=false;");
		return $oRespX;
	}

	App::loadClass('Text');
	$oTxt = &new Text();
	$oDb = &Database::get();
	$oCommentService = &App::getModuleService('Comments', 'Comment');

 	
	$sStripped = strip_tags($aFormValue['val']['text'], '<img>');

	if (empty($sStripped))
    {
    	$oRespX->addScript("document.getElementById('ajax_main_image_cm').style.display='none';");
    	$oRespX->addScript("document.getElementById('ajax_main_form').style.display='';");
		$oRespX->addScript("alert('". App::format('Mod_Comments.error.no_text') ."');");
		$oRespX->addScript("sId('comment_submit_button').disabled=false;");
		return $oRespX;
	}

	$sNewText = $oTxt->preParseText($aFormValue['val']['text']);
	$sNewText = $oTxt->utf8ToUnicode($sNewText);
	
	$oEventService = &App::getModuleService('Event', 'Event');
	$iCid = $oEventService->addComments($aFormValue['ajax_id'],$sNewText);
	if(!$iCid)
	{
		$sMess = implode($oEventService->getErrors(),' ');
		if(!$sMess)
		{
			$sMess = App::format('error');
		}
		$oRespX->addScript("document.getElementById('ajax_main_image_cm').style.display='none';");
   		$oRespX->addScript("document.getElementById('ajax_main_form').style.display='';");
		$oRespX->addScript("alert('". $sMess ."');");
		$oRespX->addScript("sId('comment_submit_button').disabled=false;");
		return $oRespX;
	}
	
	$aItem = array
	(
		'user' => App::getUser('user'),
		'text' => $sNewText,
		'time' => time(),
		'cid' => $iCid
	);
	
	$oTpl = &App::getTemplate();
	$oTpl->assignSrc(array
	(
		'aItem' => $aItem,
	));

	$sTemplate = $oTpl->fetch($oTpl->template_dir . '/_ajax/comments.html');
	$sTemplate = str_replace("'", "\'", $sTemplate);
	$sTemplate = str_replace("\r\n", "", $sTemplate);
	$sTemplate = str_replace(array("\n", "\t", "\r"), "", $sTemplate);
	$sTemplate = trim($sTemplate);	
	
	
	$oRespX->addAssign("ajax_main_image_cm","innerHTML", App::format('comment.added'));
	$oRespX->addScript("sId('new_comment_data').innerHTML = '". $sTemplate ."' + sId('new_comment_data').innerHTML;");
	$oRespX->addScript("setTimeout('sId(\'ajax_main_image_cm\').style.display=\'none\';', 4000)");
	$oRespX->addScript("new Effect.Appear('new_comment_data');");
	$oRespX->addScript("sId('total_count_comments').innerHTML=(parseInt(sId('total_count_comments').innerHTML) + 1);");
	$oRespX->addScript("sId('from_count_comments').innerHTML=(parseInt(sId('from_count_comments').innerHTML) + 1);");
	$oRespX->addScript("sId('comment_submit_button').disabled=false;");
	
	return $oRespX;
}

?>